﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Work.BL.Managers;
using Work.BL.POCOs;
using Work.DAL.Hibernate.Abstract;
using Work.DAL.Hibernate.Entities;
using Work.Tools.ObjectMapper;

namespace Work.BL.Hibernate.Managers
{
    public class HProfileEmployerManager : IProfileEmployerManager
    {
        #region Fields

        private readonly ICWSessionFactory _sessionFactory;
        private readonly IObjectMapper _objectMapper;

        #endregion

        #region Constructors

        public HProfileEmployerManager(ICWSessionFactory sessionFactory, IObjectMapper objectMapper)
        {
            this._sessionFactory = sessionFactory;
            this._objectMapper = objectMapper;
        }

        #endregion

        #region IProfileEmployee Members

        public ProfileEmployerPOCO Insert(ProfileEmployerPOCO employeePoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                ProfileEmployer employer = _objectMapper.Map<ProfileEmployerPOCO, ProfileEmployer>(employeePoco);
                session.Save(employer);
                session.Flush();
                return _objectMapper.Map<ProfileEmployer, ProfileEmployerPOCO>(employer);
            }
        }

        public List<ProfileEmployerPOCO> GetEmloyers()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<ProfileEmployer> users = session.CreateCriteria<ProfileEmployer>().List<ProfileEmployer>();
                return users
                    .Select(r => this._objectMapper.Map<ProfileEmployer, ProfileEmployerPOCO>(r))
                    .ToList();
            }
        }

        public List<ProfileEmployerPOCO> GetEmloyers(int startIndex, int maxCount)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<ProfileEmployer> projects = session.CreateCriteria<ProfileEmployer>()
                    .SetFirstResult(startIndex)
                    .SetMaxResults(maxCount)
                    .List<ProfileEmployer>();
                return projects
                    .Select(r => this._objectMapper.Map<ProfileEmployer, ProfileEmployerPOCO>(r))
                    .ToList();
            }
        }

        public int GetEmployerCount()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                int rowCount = session.CreateCriteria<ProfileEmployer>()
                    .SetProjection(Projections.RowCount()).UniqueResult<int>();
                return rowCount;
            }
        }

        public ProfileEmployerPOCO GetEmployerById(int id)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                ProfileEmployer employer = session.Get<ProfileEmployer>(id);
                return _objectMapper.Map<ProfileEmployer, ProfileEmployerPOCO>(employer);
            }
        }

        public ProfileEmployerPOCO Edit(ProfileEmployerPOCO profileEmployerPoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                ProfileEmployer employer = _objectMapper.Map<ProfileEmployerPOCO, ProfileEmployer>(profileEmployerPoco);
                session.Update(employer);
                session.Flush();
                return _objectMapper.Map<ProfileEmployer, ProfileEmployerPOCO>(employer);
            }
        }

        #endregion

        
    }
}
